<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 4. 求一个三位数，叫做“喇叭花数”，该三位数等与其每位数字的阶乘之和等于它本身。输出100~999的所有喇叭花数。

    function factorial (num) {
      if (typeof(num) !== 'number') throw new Error('请传入数字')
      var result = 1
      for (var i = num; i >= 1; i--) {
        result *= i
      }
      return result
    }
    // 检测是否是喇叭花数
    function checkTrumpt (num) {
      if (typeof(num) !== 'number' || !(num >= 100 && 100 < 1000)) throw new Error('请传入一个三位数')
      // 算出个位/十位/百位的阶乘
      var unit = num % 10
      var decade = Math.floor(num / 10) % 10
      var hundred = Math.floor(num / 100)
      // 检验和是否相等
      if (factorial(unit) + factorial(decade) + factorial(hundred) === num) return true
      return false
    }
    console.log('遍历100-999所有的喇叭花数')
    for (var i = 100; i <= 999; i++) {
      if (checkTrumpt(i)) console.log(i)
    }
  </script>
</body>
</html>